<?php
/**
 * 罚款单 2017/2/16 孟凡涛
 * 
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class FineController extends BaseController {

	/** 获取部门信息后台员工自己部门以及下属部门*/
	public function index(){
        $Mdo = D("Sector");
		$wheres = array();
		$wheres['is_deleted'] = array('eq', $Mdo::STATE_OK );
		$secs = $Mdo->field("sector_id,info,sector_name,parent_sector")->where( $wheres )->select();
        $_secs = array();
        foreach( $secs as $sc ){
            $_secs[$sc['sector_id']] = $sc;
        }
        if( 1 == $this->m_user['user_id'] ){
            $arr = list_to_tree($secs,'sector_id','parent_sector','_child', 0 );
        }
        else{
            /**
             * BUG FIX:增加自己负责的部门
             * @var [type]
             */
            $arr = array();
            $arr[]=$_secs[$this->m_user['sector_id']];
		    $arr[0]['_child'] = list_to_tree($secs,'sector_id','parent_sector','_child', $this->m_user['sector_id']);
        }
		$re = array('rcode'=>1,'data'=>0,'msg'=>'ok');
		$re['data'] = $arr;

		e_json( $re );
	}

    /*获取罚款单信息 */
    public function getFine() {

        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');

        $MFine = M('\Admin\Model\FineModel:fine');
        $dlist = $MFine ->getFine($_GET,$this->m_is_admin);

		if( $dlist['r'] ){
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
			$res['pall'] = $dlist['pall'];
			$res['p'] = $dlist['p'];
			$res['psize'] = $dlist['psize'];
		}else{
			$res['msg'] = $dlist['m'];
		}
		e_json( $res );
    }

	/*添加罚款单信息*/
	public function addFine() {
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$POST = $_POST;
        $MFine = M('\Admin\Model\FineModel:fine');
        $dlist = $MFine ->addFine($POST);
		if( $dlist['r'] ){
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
		}else{
			$res['msg'] = $dlist['m'];
		}
		e_json( $res );
	}

	/*编辑罚款单信息*/
	public function updateFine() {
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');

        $MFine = M('\Admin\Model\FineModel:fine');
        $dlist = $MFine ->updateFine($_POST);

		if( $dlist['r'] ){
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
		}else{
			$res['msg'] = $dlist['m'];
		}
		e_json( $res );
	}

	/*删除罚款单信息*/
	public function delFine() {
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');

        $MFine = M('\Admin\Model\FineModel:fine');
        $dlist = $MFine ->delFine($_POST);

		if( $dlist['r'] ){
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
		}else{
			$res['msg'] = $dlist['m'];
		}
		e_json( $res );
	}

	/*导出罚款单信息*/
	public  function exportFine() {

		Vendor('PHPExcel.PHPExcel');
		Vendor("PHPExcel.PHPExcel.IOFactory");
		$objPHPExcel = new \PHPExcel();
		// Set properties  
		$objPHPExcel->getProperties()->setCreator("slackck")//创建人
		->setLastModifiedBy("slackck")//最后修改人
		->setTitle("数据EXCEL导出")//标题
		->setSubject("数据EXCEL导出")//题目
		->setDescription("和众汇富")//描述
		->setKeywords("excel")//关键字
		->setCategory("result file");//种类
		
		// set table header content  
		$objPHPExcel->setActiveSheetIndex(0)
		->setCellValue('A1', '风控号')
		->setCellValue('B1', '违规主题词')
		->setCellValue('C1', '发布时间')
		->setCellValue('D1', '业务人员')
		->setCellValue('E1', '业务人员对应经理')
		->setCellValue('F1', '业务直接所属部门')
		->setCellValue('G1', '业务人员所属团队')
		->setCellValue('H1', '处罚内容');

		$where = array();
		$GET = $_GET;
		// if($GET['start_time'] && !$GET['end_time'])             $where['b.pub_time'] = array('EGT', $GET['start_time']);
		// if($GET['end_time'] && !$GET['start_time'])             $where['b.pub_time'] = array('ELT', $GET['end_time']);
		// if($GET['start_time'] && $GET['end_time'])              $where['b.pub_time'] = array('BETWEEN',array($GET['start_time'],$GET['end_time']));
		$start_time = strtotime($GET['start_time'] );
		$end_time = strtotime($GET['end_time'] );
		if($GET['start_time'] && $GET['end_time'] && $GET['start_time'] != 'undefined' && $GET['end_time'] != 'undefined') {
			$where['b.pub_time'] = array('BETWEEN',array(date('Y-m-d 00:00:00', $start_time ),date('Y-m-d 23:59:59', $end_time )));
		} else if($GET['start_time'] && $GET['start_time'] != 'undefined') {
			$where['b.pub_time'] = array('EGT', date('Y-m-d 00:00:00', $start_time ));
		} else if($GET['end_time'] && $GET['end_time'] != 'undefined') {
			$where['b.pub_time'] = array('ELT',date('Y-m-d 23:59:59', $end_time ));
		}
        // if($GET['sales_id'])           $where['a.sales_id'] = array('EQ', $GET['sales_id']);
		if($GET['sales_name'])            $where['c.nickname'] = array('like','%'.$GET['sales_name'].'%'); 
        $where['a.is_delete'] = 0;
        $where['b.is_delete'] = 0;
        $where['b.fine_type'] = 1;
        $where['_string'] = '1=1';

		$res = M()->field("a.fine_user_id,b.fine_num,b.theme,b.pub_time,c.nickname as salesor,d.nickname as manager,e.sector_name as sector,f.sector_name as company,g.detail_name,
		
						CASE WHEN a.fine_type = 1 THEN a.fine_mount
							 WHEN a.fine_type = 2 THEN a.fine_remark
							 ELSE '尚未确定' END AS fine_content
					")
					->table('fine_user as a')
					->join('fine as b on b.fine_id = a.fine_id')
					->join('user as c on c.user_id = a.sales_id')
					->join('user as d on d.user_id = a.sales_manager')
					->join('sector as e on e.sector_id = a.sales_sector')
					->join('sector as f on f.sector_id = a.sales_subcompany')
					->join('sys_param_detail as g on g.sys_param_detail_id = b.theme')
					->where($where)
					->order('b.pub_time desc')
					->select();

		// Miscellaneous glyphs, UTF-8
		for($i=0;$i<=count($res)-1;$i++){
			$objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+2), $res[$i]['fine_num']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+2), $res[$i]['detail_name']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+2), $res[$i]['pub_time']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+2), $res[$i]['salesor']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+2), $res[$i]['manager']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('F'.($i+2), $res[$i]['sector']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('G'.($i+2), $res[$i]['company']);
			$objPHPExcel->getActiveSheet(0)->setCellValue('H'.($i+2), $res[$i]['fine_content']);
		}
	
		//sheet命名  
		$objPHPExcel->getActiveSheet()->setTitle('罚款单结果汇总');
	
		// Set active sheet index to the first sheet, so Excel opens this as the first sheet  
		$objPHPExcel->setActiveSheetIndex(0);  
	
		//excel头参数  
		//header("Content-Type: application/vnd.ms-excel; charset=gb2312"); 
		//header('Content-Disposition: attachment;filename="'.$random.'.xls"');  //日期为文件名后缀  
		//header('Cache-Control: max-age=0');  
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5为xls格式，excel2007为xlsx格式 
		//$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel); 	
		ob_end_clean(); // Added by me
		ob_start(); // Added by me        
		$objWriter->save('php://output'); 
		// $objWriter->save('D:/'.$random.''.'.xls'); //这块修改路径。路径写到哪里，就会保存到什么地方。
	}

	/*根据部门ID获取员工信息*/
	public function getUserByID() {
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');

		$sector_id = I('get.sector_id','','intval');
		
        $MFine = M('\Admin\Model\FineModel:user');
        $dlist = $MFine ->getUserByID($sector_id);

		if( $dlist['r'] ){
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
			$res['msg'] = $dlist['m'];
		}
		e_json( $res );
	}

	/*获取违规系统参数表*/
	public function getSysParam() {
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');

		$key_name = I('get.key_name','','trim');

		$MSys = M('\Admin\Model\FineModel:param','sys_');
		$dlist = $MSys ->getSysParam($key_name);
		if($dlist) {
			$res['rcode'] = 1;
			$res['data'] = $dlist['d'];
		} else {
			$res['msg'] = $dlist['m'];
		}

		e_json($res);
	}

	/*获取富文本图片*/
	public function getImage() {
		$res = array('rcode'=>0,'data'=>NULL,'msg'=>'OK');
		
		if($_FILES){
			$upload = new \Vendor\Upload\Upload();
			$upload->maxSize   =     3145728;
			$upload->exts      =     array('png', 'jpg');  
			$upload->rootPath  =     './../www/uploads';
			$upload->savePath  =      '/image/';  
			$info   =   $upload->upload();
			if(!$info){
				$this->error($upload->getError());
			}else{
				$filename='http://www.crm3.com/uploads'.$info['wangEditorH5File']['savepath'].$info['wangEditorH5File']['savename'];
				$res['rcode'] = 1;
				$res['data'] = $filename;
				$res['url'] = $info;
			}
		}
		echo $res['data'];
		exit();
		// e_json($res['data']);
	}
	



}